package com.kye.express.business.wechat_redpacket;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.business.verification.ExchangeVerificationService;
import com.kye.express.databinding.ActivityWeChatPacketExchangeBinding;
import com.kye.express.utils.ToolbarManager;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.lib.view.statuslayout.StatusLayout;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.wechat.WechatExchangableMoneyBean;
import com.kye.model.bean.wechat.WechatWechatOpenIdAddressBean;

import io.reactivex.functions.Consumer;

public class WeChatPacketExchangeActivity extends BaseToolbarActivity implements WeChatPacketExchangeConstract.WeChatPacketExchangeView, ExchangeVerificationService.OnVerificationCheckListener, ExchangeVerificationService.OnVerificationOptionByAutoSendListener, ExchangeVerificationService.OnVerificationOptionByPictureSmsListener {

    private ActivityWeChatPacketExchangeBinding binding;
    private WeChatPacketExchangeConstract.WeChatPacketExchangePresenter presenter;
    private ExchangeVerificationService exchangeVerificationService;
    private Dialog dialog; // loading dialog
    private User userInfo;
    private String money;

    public static void startActivity(Context context) {
        checkLogin(context, new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(context, WeChatPacketExchangeActivity.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_we_chat_packet_exchange);
        presenter = new WeChatPacketExchangeConstract.WeChatPacketExchangePresenter();
        ToolbarManager.setToolbar(this, binding.toolbar);
        initViews();
        initEvents();
        initData();
    }

    private void initViews() {
        RxView.clicks(binding.llErrorServer.btErrorServerRetry)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(binding.llErrorNet.btErrorNetRetry)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        binding.statusLayout.showLoadingView();
                    }
                });
        RxView.clicks(binding.exchange)
                .compose(new DebounceObservableTransformer<>())
                .compose(bindToLifecycleDestroy())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        // todo :
                        if (Float.valueOf(money) <= 0) {
                            ToastManager.show(WeChatPacketExchangeActivity.this.getApplicationContext(), "无可兑换金额");
                            return;
                        }
                        presenter.checkBeforeExchange(userInfo);
                    }
                });
        binding.statusLayout.setOnLoadingListener(new StatusLayout.OnLoadingListener() {
            @Override
            public void onLoading() {
                presenter.getWechatExchangableMoney(userInfo);
            }
        });
    }

    private void initEvents() {

    }

    private void initData() {
        presenter.bind(this);
    }

    @Override
    public void onUser(User user) {
        userInfo = user;
        exchangeVerificationService = new ExchangeVerificationService(user.getPhone(), 2);
        exchangeVerificationService.setProvider(getProvider());
        binding.statusLayout.showLoadingView();
    }

    @Override
    public void getWeChatExchangableMoneySuccess(WechatExchangableMoneyBean wechatExchangableMoneyBean) {
        money = wechatExchangableMoneyBean.getMoney();
        binding.statusLayout.showContentView();
        binding.exchangeMoney.setText(String.format("¥%s", wechatExchangableMoneyBean.getMoney()));
    }

    @Override
    public void getWeChatExchangableMoneyNetFailed(String msg) {
        ToastManager.show(getApplicationContext(), msg);
        binding.statusLayout.showErrorNetworkView();
    }

    @Override
    public void getWeChatExchangableMoneyServerFailed(String message) {
        ToastManager.show(getApplicationContext(), message);
        binding.statusLayout.showErrorServerView();
    }

    @Override
    public void checkBeforeExchangeSuccess(WechatWechatOpenIdAddressBean wechatWechatOpenIdAddressBean) {
        // todo : 走短信验证流程
        exchangeVerificationService.check(WeChatPacketExchangeActivity.this);
    }

    @Override
    public void checkBeforeExchangeNetFailed(String msg) {
        ToastManager.show(getApplicationContext(), msg);
    }

    @Override
    public void checkBeforeExchangeServerFailed(String message) {
        ToastManager.show(getApplicationContext(), message);
    }

    /**
     * 不满足通讯录条件接口
     * @param msg
     */
    @Override
    public void onCannotExchangeError(String msg) {
        // todo : 跳转到人气支持
        ToastManager.show(getApplicationContext(), msg);
    }

    @Override
    public void onNeedSendSmsError(String msg, String smsChannel, String type, String phone) {
        // todo : 发短信验证
        exchangeVerificationService.showVerificationByAutoSendMessage(this, 3, phone, smsChannel, this);
    }

    @Override
    public void onAccountError(String msg, String smsChannel, String type, String phone) {
        // todo : 账号异常
        ToastManager.show(getApplicationContext(), msg);
    }

    @Override
    public void onOpenApiError(OpenApiResult openApiResult) {
        // todo : 服务器错误
        ToastManager.show(getApplicationContext(), openApiResult.getMsg());
    }

    @Override
    public void onOtherError(Throwable e) {
        // todo : 网络错误
        ToastManager.show(getApplicationContext(), e.getMessage());
    }

    @Override
    public void onVerifyByAutoSendMessage(String smsChannel, String type, String phone) {
        // todo : 发短信验证
        exchangeVerificationService.showVerificationByAutoSendMessage(this, 3, phone, smsChannel, this);
    }

    @Override
    public void onVerifyByPictureSms(String smsChannel, String type, String phone) {
        // todo : 动态码加短信验证
        exchangeVerificationService.showVerificationByPictureSmsDialog(this, 3, this);
    }

    @Override
    public void toExchange1(String smsCodeInput, MaterialDialog mDialog) {
        // todo : 动态码和短信验证码进行验证
        presenter.exchange(userInfo, money, smsCodeInput, mDialog);
    }

    @Override
    public void toExchange2(String smsCodeInput, MaterialDialog mDialog) {
        // todo : 发送短信获取的验证码进行验证
        presenter.exchange(userInfo, money, smsCodeInput, mDialog);
    }

    @Override
    public void getPictureCodeSuccess(byte[] picture) {

    }

    @Override
    public void onSendSmsCodeSuccess() {

    }

    @Override
    public void exchangeSuccess() {
        binding.statusLayout.showLoadingView();
    }

    @Override
    public void exchangeServerFailed(String msg) {
        ToastManager.show(getApplicationContext(), msg);
    }

    @Override
    public void exchangeNetFailed(String message) {
        ToastManager.show(getApplicationContext(), message);
    }

    @Override
    public void onError() {

    }

    @Override
    public void onError(String msg) {

    }


    @Override
    public void onLoading() {
        dialog = KyeProgressDialog.show(this, dialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(dialog);
    }

}
